//
//  YTLoginVC.m
//  CllForIOS
//
//  Created by qiyun on 2018/3/9.
//  Copyright © 2018年 aRui. All rights reserved.
//

#import "YTLoginVC.h"
#import "YTRegistVC.h"
#import "YTWebViewVC.h"
#import "YTTabBarController.h"
@interface YTLoginVC ()<UIScrollViewDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITextField * phonef;
@property (nonatomic, strong) UITextField * codef;
@end

@implementation YTLoginVC

- (BOOL)prefersStatusBarHidden {

    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    UIImageView*bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    bg.image=IMAGE(@"Loginbg");
    [self.view addSubview:bg];
    //
    UIScrollView*scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    scrollview.contentSize=CGSizeMake(SCREEN_W, SCREEN_H+1);
    scrollview.delegate=self;
    [self.view addSubview:scrollview];
    //
    UIImageView*title=[[UIImageView alloc]initWithFrame:CGRectMake(100,k_statusbarHeight+ AfW(116), AfW(91), AfW(22))];
    title.image=IMAGE(@"logintitle");
    CGPoint titlecenter=title.center;
    titlecenter.x=SCREEN_W/2;
    title.center=titlecenter;
    [scrollview addSubview:title];
    //
    UIImageView*phonebg=[[UIImageView alloc]initWithImage:IMAGE(@"loginphonebg")];
    [scrollview addSubview:phonebg];
    
    UITextField*phonef=[[UITextField alloc]init];
    phonef.delegate=self;
    [self setTextfiled:phonef withPlaceholderstr:@"请您输入11位手机号码"];
    [scrollview addSubview:phonef];
    self.phonef=phonef;
    
    UIImageView*codebg=[[UIImageView alloc]initWithImage:IMAGE(@"logincodebg")];
    [scrollview addSubview:codebg];
    
    UITextField*codef=[[UITextField alloc]init];
    codef.delegate=self;
    [self setTextfiled:codef withPlaceholderstr:@"请您输入密码"];
    codef.secureTextEntry = YES;
    [scrollview addSubview:codef];
    self.codef=codef;
    
    phonebg.frame=CGRectMake(47, CGRectGetMaxY(title.frame)+100, SCREEN_W-47*2, 44);
    codebg.frame=CGRectMake(47, CGRectGetMaxY(phonebg.frame)+22, SCREEN_W-47*2, 44);
    
    phonef.frame=CGRectMake(47+43, 20, SCREEN_W-47*2-43, 17);
    CGPoint phonefcenter=phonef.center;
    phonefcenter.y=phonebg.center.y+2;
    phonef.center=phonefcenter;
    
    codef.frame=CGRectMake(47+43, 20, SCREEN_W-47*2-43, 17);
    CGPoint codefcenter=codef.center;
    codefcenter.y=codebg.center.y+2;
    codef.center=codefcenter;
    
    //
    UIButton*registBtn=[[UIButton alloc]init];
    registBtn.titleLabel.font=KFONT(12);
    [registBtn setTitle:@"还未注册?点此注册" forState:UIControlStateNormal];
    [registBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [registBtn setTitleColor:RGBA(215, 217, 221) forState:UIControlStateNormal];
    [registBtn  addTarget:self action:@selector(toRegister) forControlEvents:UIControlEventTouchUpInside];

    [scrollview addSubview:registBtn];
    
//    UIButton*fotgetBtn=[[UIButton alloc]init];
//     fotgetBtn.titleLabel.font=KFONT(12);
//    [fotgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
//    [fotgetBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
//    [fotgetBtn setTitleColor:RGBA(215, 217, 221) forState:UIControlStateNormal];
//       [fotgetBtn  addTarget:self action:@selector(toSetCode) forControlEvents:UIControlEventTouchUpInside];
//    [scrollview addSubview:fotgetBtn];
    
    UIButton*loginBtn=[[UIButton alloc]init];
    loginBtn.titleLabel.font=KFONT_bold(15);
    [loginBtn setBackgroundImage:IMAGE(@"loginBtnbg") forState:UIControlStateNormal];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn  addTarget:self action:@selector(toLogin) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:loginBtn];
    
    UIButton*backBtn=[[UIButton alloc]init];
    backBtn.titleLabel.font=KFONT(15);
//    [backBtn setBackgroundImage:IMAGE(@"loginBtnbg") forState:UIControlStateNormal];
    [backBtn setTitle:@"返回首页" forState:UIControlStateNormal];
    [backBtn  addTarget:self action:@selector(toback) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:backBtn];
    
    
    registBtn.frame=CGRectMake(56, CGRectGetMaxY(codebg.frame)+26, 120, 12);
//    fotgetBtn.frame=CGRectMake(SCREEN_W-60-80, CGRectGetMaxY(codebg.frame)+26, 80, 12);
    loginBtn.frame=CGRectMake(47, CGRectGetMaxY(registBtn.frame)+11, SCREEN_W-47*2, 44);
    
    backBtn.frame=CGRectMake(SCREEN_W/2-50, CGRectGetMaxY(loginBtn.frame)+11, 100, 12);

    
   // NSLog(@"self.navigationController.interactivePopGestureRecognizer.enable %i",self.navigationController.interactivePopGestureRecognizer.enabled);
   
}
-(void)setTextfiled:(UITextField*)textfield withPlaceholderstr:(NSString*)str{
    
    [textfield setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [textfield setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    textfield.font = KFONT(12);
    textfield.textColor = RGBA(215, 217, 221);
    //光标颜色
    textfield.tintColor= RGBA(215, 217, 221);
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs[NSFontAttributeName] =KFONT(12); // 设置font
    attrs[NSForegroundColorAttributeName] = RGBA(215, 217, 221); // 设置颜色
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:str attributes:attrs]; // 初始化富文本占位字符串
    textfield.attributedPlaceholder = attStr;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - 点击事件
-(void)toback{
    [self dismissViewControllerAnimated:1 completion:nil];
}
-(void)toRegister{
    YTRegistVC*vc=[YTRegistVC new];
    [self.navigationController pushViewController:vc animated:1];

}
-(void)toSetCode{
    NSLog(@"%s",__func__);
}
-(void)toLogin{
    //判断手机号
    if (self.phonef.text.length <=0) {
        PROMPT(@"请输入手机号");
        return;
    }
    //判断密码
    if (self.codef.text.length <=0) {
        PROMPT(@"请输入密码");
        return;
    }
     [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *dicts = @{@"username":_phonef.text,
                            @"password":_codef.text
                            };
    __weak typeof(self) weakSelf = self;
    [SendRequest getinfoDetailWithURL:k_MainUrl(k_User_login) withDictionary:dicts result:^(NSDictionary *result, NSError *error) {
        NSLog(@"获取---%@",result);
        if ([result[@"success"] isEqual:@1] ) {
            PROMPT(result[@"msg"]);
         
            NSString*name=@"(未实名)";
            NSString*certNo=@"(未认证)";
            NSString*username=@"";
           
            if (![result[@"data"][@"name"] isEqual:[NSNull null]]) {
                //字符串为空的情况
                if ([result[@"data"][@"name"] length]>0) {
                     name=result[@"data"][@"name"];
                }
               
            }
             if (![result[@"data"][@"certNo"] isEqual:[NSNull null]]) {
                 if ([result[@"data"][@"certNo"] length]>0) {
                    certNo= [NSString stringWithFormat:@"%@******%@", [result[@"data"][@"certNo"] substringToIndex:1], [result[@"data"][@"certNo"] substringFromIndex:14]];
                 }
              
            }
            
            if (![result[@"data"][@"username"] isEqual:[NSNull null]]) {
                if ([result[@"data"][@"username"] length]>0) {
                    username=result[@"data"][@"username"];
                }
                
            }
            
            NSString*mobilestr=[result[@"data"][@"mobile"] stringByReplacingCharactersInRange:NSMakeRange(3, 5)  withString:@"*****"];
            
            //数据存本地
            NSDictionary *userdicts = @{@"account":result[@"data"][@"account"],
                                        @"whiteList2":result[@"data"][@"whiteList2"],
                                        @"mobile":mobilestr,
                                        @"password":result[@"data"][@"password"],
                                        @"autologinpassword":_codef.text,
                                        @"token":result[@"token"],
                                        @"name":name,
                                        @"certNo":certNo,
                                        @"username":username
                                        
                                        };
            [Tookit saveUserCasheInfo:[NSMutableDictionary dictionaryWithDictionary:userdicts]];
            
            [self dismissViewControllerAnimated:1 completion:nil];
            
        }else{
            PROMPT(result[@"msg"]);
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];

}
#pragma mark -dealloc

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
